<template>
    <elm-page-bb>
        <el-form ref="model" :model="model" size="small" label-width="120px">
            <elm-row>
                <el-form-item label="工作名称" prop="name" :rules="[{ required: true, message: '工作名称必须填写'}]">
                    <el-input type="name" v-model="model.name"></el-input>
                </el-form-item>
                <el-form-item label="上级">
                    <elm-select-tree v-model="model.pid" url="/business/plm/plan/task/GetTree"
                                     :url-data="{plan_code:model.plan_code}"
                                     path-url="/business/plm/plan/task/GetPath">
                    </elm-select-tree>
                </el-form-item>
            </elm-row>
            <elm-row>
                <el-form-item label="计划开始时间" prop="jh_start_time">
                    <el-date-picker v-model="model.jh_start_time" type="datetime" value-format="yyyy-MM-ddTHH:mm:ss" style="width: 100%"></el-date-picker>
                </el-form-item>
                <el-form-item label="计划结束时间" prop="jh_end_time">
                    <el-date-picker v-model="model.jh_end_time" type="datetime" value-format="yyyy-MM-ddTHH:mm:ss" style="width: 100%"></el-date-picker>
                </el-form-item>
            </elm-row>
            <elm-row>
                <el-form-item label="实际开始时间" prop="sj_start_time">
                    <el-date-picker v-model="model.sj_start_time" type="datetime" value-format="yyyy-MM-ddTHH:mm:ss" style="width: 100%"></el-date-picker>
                </el-form-item>
                <el-form-item label="实际结束时间" prop="sj_end_time">
                    <el-date-picker v-model="model.sj_end_time" type="datetime" value-format="yyyy-MM-ddTHH:mm:ss" style="width: 100%"></el-date-picker>
                </el-form-item>
            </elm-row>
            <elm-row>
                <el-form-item label="负责人" prop="assignee_id">
                    <elm-ei v-model="model.assignee_id"></elm-ei>
                </el-form-item>
                <el-form-item label="优先级" prop="priority" :rules="[{ required: true, message: '必须填写'}]">
                    <el-select v-model="model.priority" style="width: 100%">
                        <el-option v-for="item in priority_arr" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                </el-form-item>
            </elm-row>
            <elm-row>
                <el-form-item label="状态" prop="status">
                    <el-select v-model="model.status" style="width: 100%">
                        <el-option v-for="item in status_arr" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="顺序" prop="sort" :rules="[{ required: true, message: '顺序必须填写'},{ type: 'number', message: '顺序必须为数字'}]">
                    <el-input v-model.number="model.sort"></el-input>
                </el-form-item>
            </elm-row>
            <elm-row>
              <el-form-item label="关联产品" prop="pd_code">
                <el-select v-model="model.pd_code" style="width: 100%;" clearable>
                  <el-option v-for="item in opt" :value="item.sys_code_key" :label="item.name"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item v-if="!model.pd_code"></el-form-item>
              <el-form-item label="关联工序" prop="craft" v-if="model.pd_code" :key="model.pd_code">
                <elm-select-tree v-model="model.craft" url="/business/plm/product/craft/GetTree" path-url="/business/plm/product/craft/GetPath" :url-data="{pd_code:model.pd_code}"></elm-select-tree>
              </el-form-item>
            </elm-row>
            <el-form-item label="任务说明">
                <el-input type="textarea" v-model="model.description" :rows="2"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="model.remark" :rows="2"></el-input>
            </el-form-item>
        </el-form>
        <template #bottom>
            <el-button type="success" size="small" @click="submit" model.>立即保存</el-button>
            <el-button size="small" @click="close">取消</el-button>
        </template>
    </elm-page-bb>
</template>

<script>
export default {
    extends: elmVueBase,
    data() {
        return {
            model: {
              pid: elm.dialog.request.pid,
              plan_code:elm.dialog.request.plan_code,
              status:'待开始'
            },
            priority_arr: ['高','中','低'],
            status_arr: ['待开始','进行中','已完成','已延期'],
            opt:[]
        }
    },
    async created() {
      await this.getPd();
    },
    watch:{
      'model.pd_code':{
        handler(newVal,oldVal){
          if(newVal!=oldVal)
            delete this.model.craft;
        },
        deep:true
      }
    },
    methods: {
        async submit() {
          console.log(this.model);
            await this.$refs["model"].validate();
            var res = await elm.post('/business/plm/plan/task/Upsert', this.model);
            if (res == true) {
                this.close();
                this.v('保存成功。');
                this.request.callVue.treebinding();
            }
            else {
                this.error('保存失败，请稍后尝试。');
            }
        },
      async getPd(){
        this.opt=await elm.post('/business/plm/plan/task/GetProduct', {});
      }
    }
}
</script>

<style scoped>
</style>